#ifdef USAGECOROUTINE

#ifdef __linux__

#include "coroutineSocket.h"

static int InitSocket(int basefd,int offsetfd)
{
    int fd = socket(PF_INET,SOCK_STREAM,0);

    struct sockaddr_in lister;
    lister.sin_family = AF_INET;
    lister.sin_addr.s_addr = INADDR_ANY;
    lister.sin_port = htons(basefd + offsetfd);
    bind(fd,(struct sockaddr*)&lister,sizeof(lister));

    listen(fd,10);
    return fd;
}

static void accept_cb(void *arg)
{
    Scheduler *s = GetInstance_crt(); // 获取全局调度器
    Coroutine *co = s->coroutines[s->current_id];

    struct sockaddr_in addr;
    int len = sizeof(struct sockaddr_in);
    accept(co->fd, (struct sockaddr *)&addr, &len);
}

int SetEpoll()
{
    init_hooks();

    int epfd = epoll_create(1);
    for (int i = 0; i < LISTENPORTNUMBER; i++)
    {
        int fd = InitSocket(STARTPORT,i);
        
        int id = coroutine_create(fd,accept_cb,(void*)0);
        
        struct epoll_event ev;
        ev.data.fd = id;
        ev.events = EPOLLIN;
        epoll_ctl(epfd,EPOLL_CTL_ADD,fd,&ev);
    }   
    return epfd;
}

#endif
#endif